package com.otherTest.java8.tomap;
import org.junit.Test;

import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * Collectors类的三个toMap（）
 * @see <a href="http://www.java2s.com/Tutorials/Java/Java_Stream/0290__Java_Stream_Collect_to_Map.htm" />
 * @Description: 测试lambda toMap
 * @author: conceit
 * @Date: 2018/7/20 0020
 */
@SuppressWarnings("all")
public class TestToMap {

    /**
     * @Description: 将指定的值作为map的key,value
     * 第一个版本有两个参数。两个参数都是一个函数。
     * 第一个参数将流元素映射到图中的键。
     * 第二个参数将流元素映射到图中的值。
     * 如果找到重复的键，则抛出IllegalStateException
     * toMap（Function<？super T，？extends K> keyMapper，Function <？super T，？extends U> valueMapper）
     * @param:
     * @return:
     * @author: conceit
     * @Date: 2018/7/20 0020
     */

    @Test
    public void doTestExampleOne(){
       Map<Long,String> persinMap= TestToMap.persons().stream().collect(Collectors.toMap(Employee::getId,Employee::getName));
       System.out.println(persinMap);
    }


    /**
     * @Description: 将指定的值和对象(圈起来，考试要考)作为map的key,value
     * @author: conceit
     * @Date: 2018/9/11 14:48
     */
    @Test
    public void doTestExampleTwo(){
        //
        Map<Long,Employee> persinMap= TestToMap.persons().stream().collect(Collectors.toMap(Employee::getId,Employee->Employee));
        System.out.println(persinMap);
        //可以使用重默认方法
        Map<Long,Employee> defaultPersinMap= TestToMap.persons().stream().collect(Collectors.toMap(Employee::getId,Function.identity()));
        System.out.println(defaultPersinMap);

        //如果key有重复的话，会抛出异常java.lang.IllegalStateException: Duplicate key
        //可以使用重载方法，可以传入一个合并的函数来解决key冲突
        Map<Long,Employee> duplicateKeyPersinMap= TestToMap.persons().stream().collect(Collectors.toMap(Employee::getId,Function.identity(),(key1,key2)->key2));

        //可以指定一个Map的具体实现
        Map<Long,Employee> persinLinkedhashMap= TestToMap.persons().stream().collect(Collectors.toMap(Employee::getId,Function.identity(),(key1,key2)->key2,LinkedHashMap::new));
        System.out.println(persinLinkedhashMap);

        //删除map中的对象，并返回将要删除的对象
        //Employee employee=persinMap.remove(new Long(1));
        //System.out.println(employee.getId());


    }

    public static List<Employee> persons() {
        Employee p1 = new Employee(1, "Jake", Employee.Gender.MALE, LocalDate.of(1971,
                Month.JANUARY, 1), 2343.0);
        Employee p2 = new Employee(2, "Jack", Employee.Gender.MALE, LocalDate.of(1972,
                Month.JULY, 21), 7100.0);
        Employee p3 = new Employee(3, "Jane", Employee.Gender.FEMALE, LocalDate.of(1973,
                Month.MAY, 29), 5455.0);
        Employee p4 = new Employee(4, "Jode", Employee.Gender.MALE, LocalDate.of(1974,
                Month.OCTOBER, 16), 1800.0);
        Employee p5 = new Employee(5, "Jeny", Employee.Gender.FEMALE, LocalDate.of(1975,
                Month.DECEMBER, 13), 1234.0);
        Employee p6 = new Employee(6, "Jason", Employee.Gender.MALE, LocalDate.of(1976,
                Month.JUNE, 9), 3211.0);

        List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);

        return persons;
    }

    static class Employee {

        public enum Gender {
            MALE, FEMALE
        }


        private long id;
        private String name;
        private Gender gender;
        private LocalDate dob;
        private double income;

        public Employee(long id, String name, Gender gender, LocalDate dob,
                        double income) {
            this.id = id;
            this.name = name;
            this.gender = gender;
            this.dob = dob;
            this.income = income;
        }

        public long getId() {
            return id;
        }

        public String getName() {
            return name;
        }

    }

}
